/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.tools.DragEditPartsTracker;

import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;
import edu.pku.sei.modeler.editpolicies.SequenceNodeEditPolicy;
import edu.pku.sei.modeler.figures.StateInvariantFigure;
import edu.pku.sei.modeler.figures.UseCaseFigure;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.uml.BasicInteractions.StateInvariant;


/**
 * @generated
 *
 */
public class StateInvariantEditpart extends AbstractUMLNodeEditPart 
{
	public StateInvariantEditpart (MetaShapeElement model) {
		super(model);
	}
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		return new UseCaseFigure();
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		return null;
	}
	
	@Override
	protected IFigure getDirectEditFigure() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new SequenceNodeEditPolicy());
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		refreshVisuals();
		super.handlePropertyChanged(event);
	}
	
	private StateInvariant getModelElement() {
		return (StateInvariant)((MetaNode)getModel()).getModelElement();
	}
	
	protected void refreshVisuals() {
		((UseCaseFigure)this.getFigure()).setName(this.getModelElement().getName());
		super.refreshVisuals();
		
	}
	
	@Override
	public DragTracker getDragTracker(Request request) {
		return new DragEditPartsTracker(this) {
			@Override
			protected void updateTargetRequest() {
				super.updateTargetRequest();
				ChangeBoundsRequest request = (ChangeBoundsRequest)getTargetRequest();
				request.getMoveDelta().x = 0;
			}
		};
	}
}
